﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.Configuration;
using System.Text;

using Recon.Portal.Business;
using Recon.Portal.DataObjects;

namespace Recon.Portal.WebUI
{
    public partial class ReportWizard : System.Web.UI.Page
    {
        private readonly string _invoiceFields = Convert.ToString(WebConfigurationManager.AppSettings["InvoiceFields"]);
        private readonly string _statementFields = Convert.ToString(WebConfigurationManager.AppSettings["StatementFields"]);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Lists lstEntity = new Lists();

                //populate customer list
                List<Customer> customers = lstEntity.GetCustomerList();
                ddlCustomer.DataSource = customers;
                ddlCustomer.DataBind();

                //if customer specific user then select customer and disable selection
                User currentUser = ((User)Session["CurrentUserVO"]);
                if (currentUser != null)
                {
                    if (currentUser.UserType == "S")
                    {
                        ddlCustomer.Items.Insert(0, new ListItem("All Customers", "SYSTEM"));
                        ddlRemitter.Items.Insert(0, new ListItem("All Remitters", "SYSTEM"));
                        ddlRemitter.Enabled = false;
                    }
                    else
                    {
                        foreach (ListItem item in ddlCustomer.Items)
                        {
                            if (item.Value == currentUser.CustomerID)
                            {
                                item.Selected = true;
                                break;
                            }
                        }
                        ddlCustomer.Enabled = false;

                        //populate remitter list
                        RemitterEntity remitEntity = new RemitterEntity();
                        List<Remitter> remitters = remitEntity.GetRemitterIdForMatchingCriteria(ddlCustomer.SelectedValue);
                        ddlRemitter.DataSource = remitters;
                        ddlRemitter.DataBind();

                        //if remitter type user then select remitter and disable selection
                        if (currentUser.UserType == "C")
                        {
                            ddlRemitter.Items.Insert(0, new ListItem("All Remitters", "SYSTEM"));
                        }
                        else
                        {
                            foreach (ListItem item in ddlRemitter.Items)
                            {
                                if (item.Value == currentUser.RemitterID)
                                {
                                    item.Selected = true;
                                    break;
                                }
                            }
                            ddlRemitter.Enabled = false;
                        }
                    }
                }
            }
        }

        protected void wzReportCreation_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            ReportSearchCriteria criteria = new ReportSearchCriteria();
            criteria.Title = txtReportTitle.Text;
            criteria.CustomerID = ddlCustomer.SelectedValue;
            criteria.RemitterID = ddlRemitter.SelectedValue;
            if (string.IsNullOrEmpty(txtFromDate.Text))
                criteria.FromDate = DateTime.Now;
            else
            criteria.FromDate = Convert.ToDateTime(txtFromDate.Text);

            if (string.IsNullOrEmpty(txtToDate.Text))
                criteria.ToDate = DateTime.Now;
            else
                criteria.ToDate = Convert.ToDateTime(txtToDate.Text);

            //report type
            switch (rdChoice.SelectedValue)
            {
                case "I":
                    criteria.ReportType = ReportSearchCriteria.Type.Invoice;
                    break;
                case "S":
                    criteria.ReportType = ReportSearchCriteria.Type.Statement;
                    break;
                case "B":
                    criteria.ReportType = ReportSearchCriteria.Type.Both;
                    break;
            }

            //status of items to be selected in report
            criteria.Statuses = new List<ReportSearchCriteria.Status>();
            foreach (ListItem item in chkStatuses.Items)
            {
                if (item.Selected)
                {
                    switch (item.Value)
                    {
                        case "N":
                            criteria.Statuses.Add(ReportSearchCriteria.Status.New);
                            break;
                        case "S":
                            criteria.Statuses.Add(ReportSearchCriteria.Status.SendToRemitter);
                            break;
                        case "M":
                            criteria.Statuses.Add(ReportSearchCriteria.Status.Matched);
                            break;
                        case "P":
                            criteria.Statuses.Add(ReportSearchCriteria.Status.PartiallyMatched);
                            break;
                        case "A":
                            criteria.Statuses.Add(ReportSearchCriteria.Status.Authorized);
                            break;
                    }
                }
            }

            //report fields
            criteria.FieldList = new Dictionary<string, string>();
            foreach (ListItem item in chkFields.Items)
            {
                if (item.Selected)
                {
                    criteria.FieldList.Add(item.Value, item.Text);
                }
            }

            Session.Add("ReportSearchCriteria", criteria);

             /*Zujar-commented-24/02/2014: crystal report needs be displayed
              string strredir;
            strredir = "ExcelReport.aspx?";
            strredir = "<script>window.open('" + strredir + "','_blank','status=yes,toolbar=no,location=no,resizable=yes,scrollbars=1','');</script>";
            Response.Write(strredir);
              */
           Response.Redirect("Reports.aspx");
        }

        protected void wzReportCreation_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {

        }

        protected void wzReportCreation_PreviousButtonClick(object sender, WizardNavigationEventArgs e)
        {

        }

        protected void wzReportCreation_CancelButtonClick(object sender, EventArgs e)
        {

        }

        protected void rdChoice_SelectedIndexChanged(object sender, EventArgs e)
        {
            chkFields.Items.Clear();
            lblFieldText.Visible = true;

            //fields as chosen by user
            string fields = string.Empty;
            switch (rdChoice.SelectedValue)
            {
                case "I":
                    fields = _invoiceFields;
                    break;
                case "S":
                    fields = _statementFields;
                    break;
                case "B":
                    fields = _invoiceFields + "," + _statementFields;
                    break;
            }

            //split based on comma and create a field list
            List<string> lstFields = fields.Split(',').ToList<string>();
            foreach (string field in lstFields)
            {
                string[] strField = field.Split(':');
                chkFields.Items.Add(new ListItem(strField[1], strField[0]));
            }
        }
    }
}